package codeRandomThoughts.Test513找树左下角的值;

import codeRandomThoughts.utils.TreeNode;

public class Solution2 {
    int maxDepth = Integer.MIN_VALUE;
    int res = 0;

    public int findBottomLeftValue(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int depth = 0;
        findBottomLeft(root, depth);
        return res;
    }

    void findBottomLeft(TreeNode root, int depth) {
        if (root.left == null && root.right == null) {
            if (depth > maxDepth) {
                maxDepth = depth;
                res = root.val;
            }
        }

        //遍历左节点
        if (root.left != null) {
            findBottomLeft(root.left, depth + 1);
        }
        if (root.right != null) {
            findBottomLeft(root.right, depth + 1);
        }
    }

}
